spring的jdbcTemplate.batchUpdate()执行慢 |
您所在的位置:网站首页 › batchupdate oracle › spring的jdbcTemplate.batchUpdate()执行慢 |
关于jdbcTemplate.batchUpdate()执行慢的原因
公司有个需求,要对mysql生产库中的400W+数据进行洗库,做批量update操作 。于是果断选择了jdbcTemplate.batchUpdate()。测试库也就2000多条记录。然后迫不及待的开始码了
上代码吧 // 数据库连接 # jdbc spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8 spring.datasource..username=root spring.datasource..password=root spring.datasource..initialSize=5 spring.datasource..minIdle=1 spring.datasource..maxActive=20 spring.datasource..maxWait=60000 spring.datasource..timeBetweenEvictionRunsMillis=60000 spring.datasource..minEvictableIdleTimeMillis=300000 @Autowired private JdbcTemplate jdbcTemplate ... // 核心代码如下 jdbcTemplate.batchUpdate(sql.toString(), new BatchPreparedStatementSetter() { @Override public void setValues(PreparedStatement pstmt, int i) { VehicleInfo vehicleInfo = list.get(i); try{ pstmt.setString(1, user.getUserName()); ... }catch (Exception e){ e.printStackTrace(); } } @Override public int getBatchSize() { return list.size(); } });测试环境才2500+条记录就执行了13秒多?有问题啊 然后打开batchUpdate的源码一探究竟! 注意:MySQL Jdbc驱动保证5.1.13以上版本的驱动,才能实现高性能的批量插入 。在批量更新操作中,如果数据大于3条才会批量执行,否则也是会单条执行的,这个可以到源码com.mysql.jdbc.PreparedStatement中查看。 修改参数 // 数据库连接 spring.datasource.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf-8&rewriteBatchedStatements=true ...重新执行批量操作,果然,测试环境2000+条数据只用0.5s ,对比一下效果还是很显著的。 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |